package com.isunland.managesystem.base;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import butterknife.ButterKnife;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 3/30/2017 9:34 AM
 */

public abstract class BaseMultiChoiceAdapter<T> extends BaseAdapter {
  protected Context context;
  protected LayoutInflater layoutInflater;
  protected Map<T, Boolean> isCheckMap;
  protected List<T> mList = new ArrayList<>();

  public BaseMultiChoiceAdapter(Context context, ArrayList<T> objects, Map<T, Boolean> selects) {
    this.context = context;
    this.layoutInflater = LayoutInflater.from(context);
    this.mList = objects;
    this.isCheckMap = selects == null ? new HashMap<T, Boolean>() : selects;
  }

  @Override public int getCount() {
    return mList.size();
  }

  @Override public T getItem(int position) {
    return mList.get(position);
  }

  @Override public long getItemId(int position) {
    return position;
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
      convertView = layoutInflater.inflate(getViewRootId(), parent, false);
      convertView.setTag(initHolder(convertView));
    }

    initView(getItem(position), (BaseViewHolder) convertView.getTag(), position);
    return convertView;
  }

  protected abstract int getViewRootId();

  @NonNull public abstract BaseViewHolder initHolder(View convertView);

  private void initView(T object, BaseViewHolder holder, int position) {

    holder.cbCheckApplyScale.setTag(object);
    // 找到需要选中的条目
    holder.cbCheckApplyScale.setChecked(isCheckedItem(object));
    holder.cbCheckApplyScale.setOnCheckedChangeListener(
        new CompoundButton.OnCheckedChangeListener() {

          @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            T checkedItem = (T) buttonView.getTag();
            isCheckMap.put(checkedItem, isChecked);
          }
        });

    init(object, holder, position);
  }

  protected abstract void init(T object, BaseViewHolder holder, int position);

  public boolean isCheckedItem(T item) {
    if (isCheckMap == null) return false;
    if (!isCheckMap.containsKey(item)) return false;
    return isCheckMap.get(item);
  }

  public ArrayList<T> getCheckedItems() {
    ArrayList<T> resultList = new ArrayList<>();
    for (Map.Entry<T, Boolean> entry : isCheckMap.entrySet()) {
      if (entry.getValue()) {
        resultList.add(entry.getKey());
      }
    }
    return resultList;
  }

  public void setlectAll() {
    for (T staff : mList) {
      isCheckMap.put(staff, true);
    }

    notifyChanged();
  }

  private void notifyChanged() {
    ArrayList<T> newList = new ArrayList<>(mList);
    mList.clear();
    mList.addAll(newList);
    notifyDataSetChanged();
  }

  public void deSetlectAll() {
    for (T staff : mList) {
      isCheckMap.put(staff, false);
    }
    notifyChanged();
  }

  //@Override
  //public boolean isEmpty() {
  //    return false;
  //}

  protected abstract class BaseViewHolder {
    CheckBox cbCheckApplyScale;

    //@BindView(R.id.iv_logo)
    //ImageView ivLogo;
    //@BindView(R.id.tv_cooperativeMemberName)
    //TextView tvCooperativeMemberName;
    //@BindView(R.id.tv_text)
    //TextView tvText;
    //@BindView(R.id.tv_regDate)
    //TextView tvRegDate;
    //@BindView(R.id.tv_regStaffName)
    //TextView tvRegStaffName;
    //@BindView(CHECKTBOX_ID)
    //TextView tvConfirmOrder;
    //CheckBox cbCheckApplyScale;
    protected BaseViewHolder(View view) {
      //cbCheckApplyScale = (CheckBox) view.findViewById(setCheckBoxId());
      ButterKnife.bind(this, view);
      cbCheckApplyScale = (CheckBox) view.findViewById(setCheckBoxId());
    }

    protected abstract int setCheckBoxId();
  }
}
